package com.hz.filemanager.ui.base;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hz.filemanager.BuildConfig;
import com.hz.filemanager.R;
import com.hz.filemanager.ad.AdLoadManager;
import com.hz.filemanager.ad.utils.AnalyticsUtils;
import com.hz.filemanager.model.FileDataManager;
import com.hz.filemanager.ui.clean.CleanResultActivity;
import com.hz.filemanager.utils.DensityUtil;
import com.hz.filemanager.utils.LogUtils;
import com.hz.filemanager.utils.Utils;
import com.hz.sdka.ada.AdListenera;
import com.hz.sdka.ada.Interstitiala;
import com.hz.sdka.commona.bean.HZAdError;

public abstract  class InterruptActivity extends BaseActivity{

    public final static String TYPE = "type";

    public final static int TYPE_CLEAN = 1;
    public final static int TYPE_BOOST = 2;
    public final static int TYPE_COOLER = 3;
    public final static int TYPE_BATTERY = 4;

    public final static int TYPE_CLEAN_REP = 11;
    public final static int TYPE_BOOST_REP = 12;
    public final static int TYPE_COOLER_REP = 13;
    public final static int TYPE_BATTERY_REP = 14;

    protected int type;
    protected boolean isQuit = false;
    protected AlertDialog stopDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showStopDialog();
            }
        });
    }

    @Override
    public void onBackPressed() {
        showStopDialog();
    }

    private void showStopDialog() {

        if(stopDialog == null){
            /*stopDialog = DialogManager.stopDialog(this, new DialogManager.OnDialogClickListener() {
                @Override
                public void onOKClick(Object data) {
                    isQuit = true;
                    finish();
                }
            });*/
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_stop, null, false);
            TextView okBt = view.findViewById(R.id.stop_btn);
            TextView cancleBt = view.findViewById(R.id.cancel_btn);

            stopDialog = new AlertDialog.Builder(this)
                    .setView(view)
                    .create();

            okBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopDialog.dismiss();
                    isQuit = true;

                    String action = getActionWithType(type);
                    LogUtils.e(TAG, "action = " + action);
                    AnalyticsUtils.INSTANCE.logEvent("zhongduan_ad_" + action);
                    if(Interstitiala.getInstance().isReady()){
                        AnalyticsUtils.INSTANCE.logEvent("zhongduan_ad_ready_" + action);

                        if(FileDataManager.appConfig.getFile_Config().getZdfh_ad()==1){
                            AdLoadManager.INSTANCE.showInterstitialAd(InterruptActivity.this, BuildConfig.CJ_INTERSTITIAL_AD, new AdListenera() {
                                @Override
                                public void adShow() {
                                    AnalyticsUtils.INSTANCE.logEvent("zhongduan_ad_succeed_" + action);
                                }

                                @Override
                                public void adClose() {

                                }

                                @Override
                                public void adError(HZAdError adError) {

                                }
                            });
                        }
                    }

                    finish();
                }
            });
            cancleBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopDialog.dismiss();
                }
            });

            stopDialog.show();
            stopDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            setDialogWidthAndHeight(stopDialog, 320,0);
        }
        if(!stopDialog.isShowing())
            stopDialog.show();
    }

    public String getActionWithType(int type) {
        String action = "";
        switch (type){
            case TYPE_CLEAN:
            case TYPE_CLEAN_REP:
                action = "ql";
                break;
            case TYPE_BOOST:
            case TYPE_BOOST_REP:
                action = "js";
                break;
            case TYPE_COOLER:
            case TYPE_COOLER_REP:
                action = "jw";
                break;
            case TYPE_BATTERY:
            case TYPE_BATTERY_REP:
                action = "sd";
                break;
        }
        return action;
    }

    @Override
    protected void onStart() {
        super.onStart();
        isQuit = false;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(stopDialog != null && stopDialog.isShowing()){
            stopDialog.dismiss();
        }
    }

    public static void setDialogWidthAndHeight(Dialog dialog, int widthDp, int heightDp) {

        dialog.getWindow().setLayout(DensityUtil.dip2px(Utils.getContext(), widthDp), ConstraintLayout.LayoutParams.WRAP_CONTENT);

    }

}
